﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using OilWaterSystem.Common;
using OilWaterSystem.Common.select;
using OilWaterSystem.IBLL;
using OilWaterSystem.Model;

namespace OilWaterSystem.BLL
{
    public partial class Tb_UserService : BaseService<Tb_User>, ITb_UserService
    {
        /// <summary>
        /// 完成了对用户的校验
        /// </summary>
        /// <param name="user">传递了用户类</param>
        /// <returns>返回了执行结果的枚举类型</returns>
        public LoginResult CheckUserInfo(Tb_User user)
        {
            //在这里会去数据库检查是否有数据，如果没有的话就会返回一个空值
            var LoginUserInfoCheck = _DbSession.Tb_UserRepository.LoadEntities(u => u.User_Name == user.User_Name).FirstOrDefault();
            //ASP.NET MVC中MD5加密跟Web Forms中不一样
            string pw = Encrypt.MD5Encrypt(user.Password);
            //对返回的结果进行判断
            if (LoginUserInfoCheck == null)
            {
                return LoginResult.UserNotExist;
            }
            else if (!String.Equals(LoginUserInfoCheck.Password , Encrypt.MD5Encrypt(user.Password)))
            {
                return LoginResult.PwdError;
            }
            else if (LoginUserInfoCheck.Role_Id == 1)
            {
                return LoginResult.AdminOK;
            }
            else
            {
                return LoginResult.NotAdminOK;
            }
        }

        /// <summary>
        /// 判断用户名不能重复
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <returns>返回执行结果的枚举类型</returns>
        public LoginResult CheckUserNameTest(string UserName)
        {
            var checkUserName = _DbSession.Tb_UserRepository.LoadEntities(u => u.User_Name == UserName).FirstOrDefault();
            if (checkUserName != null)
            {
                return LoginResult.UserExist;
            }
            else
            {
                return LoginResult.UserNotExist;
            }
        }

        /// <summary>
        /// 实现批量删除数据
        /// </summary>
        /// <param name="deleteIds">批量删除的List集合的参数</param>
        /// <returns>返回受影响的行数</returns>
        public int DeleteUsers(List<string> deleteIds)
        {
            foreach (var deleteID in deleteIds)
            {
                _DbSession.Tb_UserRepository.DeleteEntity(new Tb_User()
                {
                    User_Id = deleteID
                });
            }
            return _DbSession.SaveChanges();
        }

        /// <summary>
        /// 实现对多条件查询的判断方法的封装
        /// </summary>
        /// <param name="query">引用传递，传递参数的信息</param>
        /// <returns>返回用户类的IQueryable集合</returns>
        public IQueryable<Tb_User> LoadSearchData(UserInfoQuery query)
        {
            var temp = _DbSession.Tb_UserRepository.LoadEntities(u => true);
            //首先过滤姓名
            if (!string.IsNullOrEmpty(query.User_Id))
            {
                temp = temp.Where<Tb_User>(u => u.User_Id == query.User_Id);  //like '%mmm%'
            }
            if (!string.IsNullOrEmpty(query.Dep_Id))
            {
                temp = temp.Where<Tb_User>(u => u.Dep_Id == query.Dep_Id);
            }
            if (!string.IsNullOrEmpty(query.User_Name))
            {
                temp = temp.Where<Tb_User>(u => u.User_Name == query.User_Name);
            }
            if (query.Gender != null)
            {
                temp = temp.Where<Tb_User>(u => u.Gender == query.Gender);
            }
            if (!string.IsNullOrEmpty(query.Password))
            {
                temp = temp.Where<Tb_User>(u => u.Password == query.Password);
            }
            if (query.Role_Id != null)
            {
                temp = temp.Where<Tb_User>(u => u.Role_Id == query.Role_Id);
            }
            if (!string.IsNullOrEmpty(query.Id_Number))
            {
                temp = temp.Where<Tb_User>(u => u.Id_Number == query.Id_Number);
            }
            if (!string.IsNullOrEmpty(query.Email))
            {
                temp = temp.Where<Tb_User>(u => u.Email == query.Email);
            }
            if (!string.IsNullOrEmpty(query.Phone))
            {
                temp = temp.Where<Tb_User>(u => u.Phone == query.Phone);
            }
            if (query.Status != null)
            {
                temp = temp.Where<Tb_User>(u => u.Status == query.Status);
            }
            temp = temp.Where<Tb_User>(u => u.Dep_Id == "");
            query.Total = temp.Count();
            return temp.OrderBy(u => u.Dep_Id).Skip(query.PageSize * (query.PageIndex - 1)).Take(query.PageSize);
        }

    }
}
